
package controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import entity.Evaluation;
import service.EvaluationService;
import service.impl.EvaluationServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/evaluationController")
public class EvaluationController extends HttpServlet {
    private EvaluationService evaluationService = new EvaluationServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String op = request.getParameter("op");

        if ("add".equals(op)) {
            JSONObject result = new JSONObject();
            try {
                String orderId = request.getParameter("orderId");
                String description = request.getParameter("description");
                int rating = Integer.parseInt(request.getParameter("rating"));
                String content = request.getParameter("content");
                String tags = request.getParameter("tags");
                String name = request.getParameter("name");
                
                // 调用service层保存评价
                boolean success = evaluationService.addEvaluation(orderId,description, rating, content, tags,name);
                
                result.put("success", success);
                result.put("message", success ? "评价已保存" : "保存评价失败");
            } catch (Exception e) {
                result.put("success", false);
                result.put("message", "系统错误: " + e.getMessage());
            }
            out.write(result.toJSONString());
        }
        if ("query".equals(op)) {
            String ratingStr = request.getParameter("rating");
            String description = request.getParameter("description");
            String time = request.getParameter("time");
            String pageStr = request.getParameter("page");

            int rating = ratingStr != null && !ratingStr.isEmpty() ? Integer.parseInt(ratingStr) : 0;

            try {

                List<Evaluation> list = evaluationService.chaxun(
                    rating,
                    description,
                    time
                );

                Map<String, Object> result = new HashMap<>();
                result.put("list", list);

                out.write(JSON.toJSONString(result));
            } catch (Exception e) {
                e.printStackTrace();
                out.write("{\"error\":\"查询失败\"}");
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
}
